A to Z Docker example

September 27, 2022

Create your docker file

FROM ubuntu:latest

RUN apt-get update -y

Create image

docker build .

Create a container

Get the image list

docker image ls
docker run -d -t --name mycontainer -p 3000:3000 <image_id>

, does the container exist? Then delete it first.

Options: -t allows your container to be left running, usefull if you want to enter inside it for debugging purposes. -p wires your network, useful to (re)direct traffic to your container. Imagine running a dockerized database and redirect traffic from your host to your docker container.

Delete container

Stop the container

docker stop mycontainer

Then delete

docker rm mycontainer

Else, just force and delete

docker rm --force mycontainer

Deleting all containers and/or images

To delete all containers

docker rm --force $(docker ps -a -f status=exited -q)

To delete all images

docker rmi --force $(docker images -a -q)

Sometimes theres a dependency between containers and images that prevents their removal. To solve it keep cycling the above two commands until all is cleaned.

List containers

docker container ls

List all event (+ stopped)

docker container ls --all

Getting inside your container

docker exec -ti mycontainer /bin/bash