A to Z Docker example

December 19, 2023

Init, Build and Run

Work based on: Overview of the get started guide

Clone an get-started app:

git clone https://github.com/docker/getting-started-app.git

Create a Dockerfile for the above application:

docker init

? Do you want to overwrite them? Yes ? What application platform does your project use? Node ? What version of Node do you want to use? 14.15.0 ? Which package manager do you want to use? yarn ? What command do you want to use to start the app? node src/index.js ? What port does your server listen on? 3000

Build your docker image named docker-rust-image:

docker build -t getting-started .

Start the app container:

docker run -dp 127.0.0.1:3000:3000 getting-started

-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.

Make sure all is running:

curl -X GET localhost:3000

, or go to `http:localhost:3000

Container volumes

Work based on: Container volumes

Create a volume by using the docker volume create command.

docker volume create todo-db

Remove previous container:

  • Get the containers:
    docker ps
    
  • Get the getting-started container id:
    docker ps | grep getting-started
    
  • , finally:
    docker rm -f $(docker ps | grep getting-started | awk '{print $1}')
    

Start the app container:

docker run -dp 127.0.0.1:3000:3000 --mount type=volume,src=todo-db,target=/etc/todos getting-started

Check that the persisten values are still present after deleting and starting a new container.

Bind mounts

Work based on: Bind mounts

Let’s start our container making sure to externalize the databased file:

mkdir db

, then

docker run -dp 127.0.0.1:3000:3000 --mount type=bind,src="$(pwd)/db",target=/etc getting-started

Other(s)

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