Skip to content

Instantly share code, notes, and snippets.

@silvioramalho
Last active March 22, 2021 14:57
Show Gist options
  • Save silvioramalho/c1e5d22c4c2e4abfbf40d47e2a9f3529 to your computer and use it in GitHub Desktop.
Save silvioramalho/c1e5d22c4c2e4abfbf40d47e2a9f3529 to your computer and use it in GitHub Desktop.
Docker Commands

Common Docker Commands

Ref: https://docs.docker.com/engine/reference/commandline/docker/

Run docker (Auto download if necessary)

docker run ubuntu

Running Providing name to container

docker run -d -P --name meu-site dockersamples/static-site

Running mapping port (source:destination)

docker run -d -p 12345:80 dockersamples/static-site

Running containers with random external port (-P)

docker run -d -P dockersamples/static-site

Set env variable

docker run -d -P -e AUTHOR="Douglas Q" dockersamples/static-site

Run command inside container and return to caller

docker run ubuntu echo "Hello World"

Interactive terminal (Open terminal inside container)

docker run -it ubuntu

Running container without detached flag (No block)

docker run -d dockersamples/static-site

Show running containers

docker ps

Show all containers

docker ps -a

Start an existing container

docker start IMAGE_ID

Stop a running container

docker stop IMAGE_ID

or if a named container

docker stop CONTAINER_NAMED_WITH_FLAG

Stop all containers with one command

docker stop $(docker ps -q)

  • or without wait 10 seconds

docker stop -t 0 $(docker ps -q)

Start container with interactive terminal

docker start -a -i IMAGE_ID

Remove Container

docker rm IMAGE_ID

Force Remove

docker rm -f IMAGE_ID

Remove all inactive container

docker container prune

Show images

docker images

Remove images

docker rmi IMAGE_NAME

Download image withou run

docker pull ubuntu

docker pull user/ubuntu

docker pull user/ubuntu:version

Show ports

docker port IMAGE_ID

Volumes

Create container with volume (-v)

docker run -v "/var/www" ubuntu

or "real-folder:inside-container-folder"

docker run -it -v "C:\Users\Alura\Desktop:/var/www" ubuntu

Inspect container

docker inspect IMAGE_ID

Running a Node application in container

docker run -v "D:\dev\node-app-test:/var/www" node

Start a Node application in container

docker run -v "D:\dev\node-app-test:/var/www" node npm start

docker run -p 8080:3000 -v "D:\dev\node-app-test:/var/www" -w "/var/www" node npm start

Network

Create Network

docker network create --driver bridge my_net

Associate container to network

docker run -it --name my-ubuntu-container --network my-net ubuntu

Show network settings

docker run -it --name my-second-ubuntu-container --network my-net ubuntu

  • You can ping the containers using defined name

ping my-ubuntu-container

or

docker exec -it my-ubuntu-container ping my-second-ubuntu-container

Inspect some network

docker network inspect my-net

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment