Essential docker commands

Docker is now mainstream. After last year of intensive work with it here a few docker commands I’m using every day that you may find essential and useful in your daily work with containers.

pull

This one works just as git, get’s you image from external repository. What you need to specify is a name of the image and a tag. The tag is not required and it will be resolved to “latest” if not specified. This command will also update your local image if the tag was updated in remote registry. It’s useful when you work with with statically tagged projects and you need to update them from time to time.

This command works the same with docker-compose. The difference is that it will pull and update all the images specified in the docker-compose.yml file.

tag

Tagging is the mechanism of pinning the version of image in a human readable format. You can think about it being just like git tags. There is few rules related with tag names, for example you can’t use / or : characters as they can be a part of image name and separator between image name and tag.

To tag image with a specific name you need the original tag name or hash ID if the image. Hash id may look similar to c8c545b3z57c. To create tag for hash id of the image you’ll run command:

docker tag c8c545b3z57c darsd/test-image

You can also add a tag to an image by referencing an other tag it has. Also, if you want to upload a tag to private registry, like ECR for example, you need to tag the image with full “url” of the image

docker tag darsd/test-image registry.dariuszsadowski.com/darsd/test-image

As the tag contains domain name now the process will know where to push the image. Push is also the next command we’ll take a look at.

push

In the same manner as pull the command will push specified tag to the registry. If tag is specified in the short format (darsd/test-image) docker will push it to the project in the Docker Hub. If the tag contains URL it will be used as the target of push.

docker push darsd/test-image
docker push registry.dariuszsadowski.com/darsd/test-image

build

Now we know how to interact with the registry let’s build an image. No surprise command for that will be the build. As an argument it takes a path to a Dockerfile. How to make it is a topic for a different post.
By default you’ll build an image with a hash id mentioned before therefore you can tag it later with your tag. You can do this as well within the build command passing tags with -t flag.

docker build -t darsd/test-image -t registry.dariuszsadowski.com/darsd/test-image .

ps

This is a simple one. docker ps allows you to see the list of active containers. You’ll se the id, name, image used and other useful information, like ports.

exec -it

This is one of the most useful docker commands. IT allows you to ssh into a container (if you have shell there) and use the usual command line tools to debug the container or perform other tasks. Not that it shouldn’t be used to make any persistent changes in the container. Those things belong to the Dockerfile.

Let’s say you used an image based on debian with bash available. To get command prompt in the container you can use command like following. Instead of hash you can use name of the container (not name of the image).

docker exec -it c8c545b3z57c /bin/bash

Tip with exec is to check what shell is available in the base system used to build the image. Usually it’s /bin/bash, but for example in alpine (very popular, lightweight distribution) it’s a /bin/ash.

image ls

This command will list all the images stored on your local machine. It’s useful to see the tags you have available locally. Sometimes it may return a lot of results so you may use grep to make is shorter.

system prune

When working with docker images over time you’ll aggregate a lot of data. It will be untagged images, unused networks and containers. To clean them up and free space on your machine this command will clean all the unused resources. Flag a also clean mentioned untagged images which usually aren’t in use anyway. So the full command is

docker system prune -a

docker-compose up

Docker-compose allows you to save configuration for your container in a yaml files. It’s very useful and IMO you should use it over docker run. Given you have a docker-compose.yml file in the directory you’re in the command will start all the specified containers.

Cool thing with the up command is that you can specify exactly what services you want to run. For example you have your app, mysql database and rabbitmq broker but for the development purposes you want just queue and database. To get them without the application you can run

docker-compose up mysql rabbitmq

docker-compose down

This command will clean up containers specified in the docker-compose.yml file. Usually you’ll run it with flags –rmi all and -v to delete images and volumes used by the containers. Therefore it’s the command you want to use when you want to reset and start fresh.

docker-compose down --rmi all -v

Wrapping up

Those docker commands should get you up and running with docker and docker-compose CLIs. Those are the command I’m using the most on the daily basis. If you’re actively using docker I suggest you have a look at the official documentation for both docker and docker-compose so you’ll get more in depth knowledge and learn more about additional arguments you can use with them. They are also very useful when working with docker swarm I wrote before as the cli is basically the same with just a few additions.

About the author: dariusz

Doing code longer than I can remember. First great achievement in configuring sound card on Slackware 8. Now polyglot with Java as the main language and doing my best with *DD.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.