DEV Community

Cover image for  5 Docker most useful commands
Bartosz Gajda
Bartosz Gajda

Posted on • Originally published at bartoszgajda.com

5 Docker most useful commands

Docker is widely used containerization system. Moreover Docker can be easily name a necessary tool in software development. This post shows you some of the most useful Docker commands in your daily workflow.

1. Execute command inside container

When working with containers you might find a need to run some command inside it, to adjust some configuration and what not. For this use the following command:

docker exec -it $container_id $command

So let’s say I want to execute sh command inside my container. I do that with:

docker exec -it 9e5017aef3f9 sh

Read more here.

2. Publishing container’s ports

The simple and easy to understand command that can be useful when having fun with lots of different containers.

docker build -p $local_port:$container_port $container_id

If you are building a container with let’s say a web app, that is served at port 80, by default this port is only accessible from inside the container. If you want to expose this, and open the web app in your local browser, you can do this:

docker build -p 80:80 9e5017aef3f9

Read more here.

3. Stop all running containers

This one is required if you want to access the container inner service, from your network — either local or public.

docker stop $(docker ps -a -q)

Read more here.

4. Attach volume to the container

Volumes in Docker environment is a mechanism of persisting data generated by the containers. If there is a data used by container, that you want to keep even after container is restarted, you have to use this. The command looks like this:

docker run -d -v $local_path:$container_path $container_id

So if you would like to attach a local folder into the container, you do this using following command:

docker run -d -v /db_dump:/app/data 9e5017aef3f9

Read more here.

5. Display container’s logs

Very useful when debugging or if you just want to know what is happening inside of the container. To see them, use the following command:

docker logs -f $container_id

So the fetch logs of our test container, I would use the following:

docker logs -f 9e5017aef3f9

Read more here.

Summary

I hope you have found this post useful. If so, don’t hesitate to like or share this post. Additionally you can follow me on my social media if you fancy so 🙂

Top comments (1)

Collapse
 
edoardoc profile image
Edoardo Ceccarelli

Yes and no.3 I always "double" it with

docker rm $(docker ps -a -q)