Skip to main content

Training notes

https://www.linkedin.com/learning/paths/developing-and-delivering-software-with-docker?u=76815018

Run docker as NON-ROOT:

sudo groupadd docker
sudo usermod -aG docker $USER

Logout and login. If still fails, run:

sudo chmod 666 /var/run/docker.sock

Run hello world image

docker run hello-world

List images

docker images

Run bash from an ubuntu latest image container. Note: This won't delete the container once you exit. It will stop the container if you exit.

docker run -it ubuntu:latest bash

:latest is optional. If not informed, docker will always get latest

docker run -it ubuntu bash

--name <name> allows you to provide a name. If not informed, docker will give a name to it

docker run --name <name_for_the_container> -it ubuntu bash

Run bash from an ubuntu latest image container. Note: This WILL delete the container once you exit. It will stop the container if you exit.

docker run --rm -it ubuntu bash

Other example:

docker run --rm -it ubuntu bash -c "sleep 3; echo all done"

Run detached container. Note: It will make it run in the background.

docker run -d -it ubuntu bash

To attach to the container

docker attach <container_name or container_ID>

From within a container, use CTRL-p + CTRL-q to detach from it. You can reatach using the command above

List docker running containers

docker ps

List all docker (running or not) containers

docker ps -a

Show last created container

docker ps -l

Create an image from a container

docker commit <container_name or container_ID> <new_image_name>

Run another shell from a running container

docker exec -it <container_name or container_ID> bash

See logs of a container that somehow failed

docker logs <container_name or container_ID>

Stop a running container

docker kill <container_name or container_ID>

Remove a stopped container

docker rm <container_name or container_ID>

Restart a container automatically if it dies

docker run -d -p 5000:5000 --restart=always --name registry registry:2

Open port and use external and internal to docker as the same number

docker run -ti -p 45678:45678 ubuntu bash

To allow docker manage the external port number, do:

docker run -ti -p 45678 --name test ubuntu:latest bash

To find what ports are open, run

docker port <container_name or container_ID>

List Docker Networks

docker network ls

Create new Docker network

docker network create <network_name>

Create a container in a network

docker run --rm -ti --net <network_name> --name <container_name> ubuntu:latest bash

Delete docker images
List images first

docker images

To delete, use Image ID

docker rmi <image_name>

Persist data into a volume
Create a folder at the server

mkdir <path_to_server_folder>

Run container

docker run -ti -v /<path_to_a_local_folder>:/<path_to_container_folder> ubuntu bash

Example

docker run -ti -v /opt/share:/shared-folder ubuntu bash

Share data between containers. Note: this data is not persisted when the containers are removed
Create the container holding the volume

docker run -ti -v /shared-data --name first_container ubuntu bash

Create the container(s) that will see this volume

docker run -ti --volumes-from first_container ubuntu bash

Search docker images

docker search <keyword>

Example

docker search ubuntu

Also visit https://hub.docker.com