Docker command cheat sheet
This Docker command cheat sheet is for those who manage their Docker image and container. I intend not to mix this up with Swarm, docker-compose and docker-machine so I will create a separate cheat sheet for my next topic.
If you have not had experience on Docker containers before, please read my topic first about Why you should learn Docker.
Manage Docker Image
docker image --help docker image ls -f dangling=true
The value 6f4b0659e2ea
is the Image ID, dockerhubacct/publish
is a repository name, and sometag
is the name of the tag.
The -f dangling=true
flag will filter the list of images by showing only those that has no relationship or use with other images.
docker tag \ 6f4b0659e2ea dockerhubacct/publish:sometag
Push and Pull Docker Image
docker login -u dockerhubacct docker push dockerhubacct/publish:sometag docker pull dockerhubacct/publish:sometag
Before you can start pushing and pulling image in Docker hub you will need to login first using your account, and you can do that in the command line using docker login
command.
docker image prune docker image prune -a
The prune
command will remove the unused dangling images. And adding the -a
flag will remove all unused images, not just dangling ones.
Build an image with arguments
docker build \ --build-arg project_port=8001 \ --build-arg project_root_dir=myproject \ -t dockerhubacct/publish:sometag .
With the --build-arg
you can pass build-time variables. The -t
flag is the name and optionally a tag in the name:tag
format.
Another important part of this command is the dot ( . ) at the very end. That means the path of the Dockerfile is in the same folder location from where you are executing the command.
Archive Docker image
docker image save dockerhubacct/publish:sometag > myproject-docker-image.tar
Load image from .tar File
docker load < myproject-docker-image.tar
Manage Docker Container
docker container --help docker container ls -a docker container rm $(docker container ls -aq) docker container top mycontainer docker container rename mycontainer myoldcontainer
In these commands, the -a
flag means show all containers and the -q
will only display the numeric identification of the container.
To remove all containers with rm
command it is possible to use this in conjunction with the result of listing the container ID $(docker container ls -aq)
.
Inspecting a Docker Container
docker container inspect -f "{{ .Config.Env }}" mycontainer
docker container inspect \ -f '{{.Name}}: {{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' mycontainer
Copy and list files inside the container
docker container cp foo.text mycontainer:/var/www docker container exec mycontainer ls /var/www
The foo.text file is in the host server, copying this inside the container /var/www
folder.
Setting environment variables and run a container
docker run -e SETTINGS=development --name mycontainer dockerhubacct/publish:sometag
docker run \ --network=host --name mycontainer \ dockerhubacct/publish:sometag
Run container using the Host network. The port mappings will be disregarded when the container is running using Host network.
sudo docker container create --name mycontainer -p 8001:8001 dockerhubacct/publish:sometag
Prepare the container for running. Similar to docker run -d
except the container is never started
docker run --rm --name mycontainer --entrypoint "/bin/bash" dockerhubacct/publish:sometag somescript.sh
Override entry point specified in the Dockerfile. Run the container and execute /bin/bash
command instead. Moreover, the --rm
will remove the container when it exit.
docker exec $(docker container ls --format {{.Names}} | grep 'webapp') \ bash -c "./script.sh" >> /var/log/webapp.log
Firstly, we are passing the result of the $(docker container ls --format {{.Names}} | grep 'webapp')
to the exec
command. And the result are the names of the containers by using the --format {{.Names}}
flag.
Secondly, inside the container run a bash script script.sh
. That means all the returned containers with “webapp” in its name will run the script respectively.
Lastly, the output of the exec
command will be logged in the host server /var/log/webapp.log
.
This is useful if you have a maintenance script that you need to run within the container. For instance, you may setup a periodical task in the host server and use the docker exec
command to run the script.
sudo docker exec -it mycontainer /bin/bash
Run a command in a running container. The -i
flag will keep STDIN open even if not attached, and -t
flag will allocate a pseudo-TTY. /bin/bash
is the command that will be executed inside the container.
docker commit --change="ENV DEBUG=True PROJECT_PORT=8002" --change="EXPOSE 8002" mycontainer dockerhubacct/publish:sometagv2
Create a new image from a container’s change.
To summarize this Docker command cheat sheet, I have shown you the commands for managing Docker Image, Containers and how to execute commands inside the container.