Nono.MA

MARCH 17, 2023

docker run -it -p HOST_PORT:CONTAINER_PORT your-image

When you run services inside of Docker in specific ports, those are internal ports on the virtual container environment. If you want to connect to those services from your machine, you need to expose ports to the outside world explicitly. In short, you need to map TCP ports in the container to ports on the Docker host, which may be your computer. Here's how to do it.

Let's imagine we have a Next.js app running inside our Docker container.

› docker run -it my-app-image
next dev
# ready - started server on 0.0.0.0:3000, url: http://localhost:3000

The site is exposed to port 3000 of the container, but we can't access it from our machine at http://localhost:3000. Let's map the port.

› docker run -it -p 1234:3000 my-app-image
next dev
# ready - started server on 0.0.0.0:3000, url: http://localhost:3000
  • We've mapped TCP port 3000 of the container to port 1234 of the Docker host (our machine)
  • We can now browse the app at http://localhost:1234
  • When your machine loads port 1234, Docker forwards the communication to port 3000 of the container

FEBRUARY 1, 2023

Here's how to pass arguments to a Dockerfile when building a custom image with Docker.

First, you need to define a Dockerfile which uses an argument.

# Dockerfile
FROM python

ARG code_dir # Our argument

WORKDIR /code/
ENTRYPOINT ["python", "/code/script.py"]

COPY ./$code_dir /code/
RUN pip install -r requirements.txt

What the above Dockerfile does is parametrize the location of the directory of script.py, our Docker image's entry point. For this example's sake, let's assume our directory structure looks like the following.

project/
  Dockerfile
  code_a/script.py
  code_b/script.py
# code_a/script.py
print('This is code_a!')
# code_b/script.py
print('This is code_b!')

Then you'll pass the code_dir variable as an argument to docker build to decide whether the Dockerfile is going to COPY folder code_a or code_b into our image.

Let's pass code_a as our code_dir first.

docker build -t my_image_a --build-arg code_dir=code_a .
docker run -it my_image_a
# Prints 'This is code_a!'

Then code_b.

docker build -t my_image_b --build-arg code_dir=code_b .
docker run -it my_image_b
# Prints 'This is code_b!'

The objective of this example was to avoid having two different Dockerfiles that look exactly the same but simply specify different source code paths. We could have done the same with the following two Dockerfiles and specifying which Docker file to use in each case with the -f flag.

# Dockerfile.code_a
FROM python

WORKDIR /code/
ENTRYPOINT ["python", "/code/script.py"]

COPY ./code_a /code/
RUN pip install -r requirements.txt
# Dockerfile.code_b
FROM python

WORKDIR /code/
ENTRYPOINT ["python", "/code/script.py"]

COPY ./code_b /code/
RUN pip install -r requirements.txt
docker build -t my_image_a -f Dockerfile.code_a .
docker run -it my_image_a
# Prints 'This is code_a!'
docker build -t my_image_b --f Dockerfile.code_b .
docker run -it my_image_b
# Prints 'This is code_b!'

If you found this useful, let me know!

NOVEMBER 9, 2022


How to use TensorFlow inside of a Docker container.


See transcript ›

LAST UPDATED FEBRUARY 6, 2024

For a Docker container started with the docker run command to be removed on exit, you need to use the --rm flag.

docker run -it --rm ubuntu /bin/bash

If you forgot to run your container with --rm, you can list running containers and close them.

List Docker containers

# The -a flag shows exited containers as well.
docker container ls -a
# CONTAINER ID  IMAGE   COMMAND      CREATED      STATUS      NAMES
# c88ccae63e88  ubuntu  "/bin/bash"  20 secs ago  Exited (0)  pop_low

Delete a stopped container by ID.

docker rm c88ccae63e88 # or docker container rm c88ccae63e88
# c88ccae63e88

You can't remove a running up container

Here's the error you'll get if you try to delete a running container. You have to stop the container before removing it or force remove it.

docker container rm db39950ab550
Error response from daemon: You cannot remove a running container db39950ab55019fcb0b7eadaacb777f9babd6f8198c1082e5621980055c0eaa9. Stop the container before attempting removal or force remove

Force remove a running container

# The -f flag force-removes running containers.
docker rm -f db39950ab550 # or docker container rm -f db39950ab550
# db39950ab550

JUNE 9, 2022

"The following command runs an ubuntu container, attaches interactively to your local command-line session, and runs /bin/bash," reads the official Docker starter guide.

docker run -it ubuntu /bin/bash

Inspecting the Linux virtual machine

docker run -it ubuntu /bin/bash

# List files inside of the Docker container
root@642064598df6:/ ls
# bin   dev  home  lib32  libx32  mnt  proc  run   srv  tmp  var
# boot  etc  lib   lib64  media   opt  root  sbin  sys  usr

# Print the current directory
root@642064598df6:/ pwd
# /

# Exit the instance
root@642064598df6:/ exit
# exit

Behind the scenes

Here's a summary from Docker's docs.

When you run this command, the following happens (assuming you are using the default registry configuration):

  • If you don't have the ubuntu image locally, Docker pulls it from your configured registry (as if you had run docker pull ubuntu).
  • Docker creates a new container (as if you had run a docker container create command manually).
  • Docker allocates a read-write filesystem to the container as its final layer. This allows a running container to create or modify files and directories in its local filesystem.
  • Docker creates a network interface to connect the container to the default network, since you did not specify any networking options. This includes assigning an IP address to the container. By default, containers can connect to external networks using the host machine's network connection.
  • Docker starts the container and executes /bin/bash. Because the container is running interactively and attached to your terminal (due to the -i and -t flags), you can provide input using your keyboard while the output is logged to your terminal.

When you type exit to terminate the /bin/bash command, the container stops but is not removed. You can start it again or remove it.

Read the Docker overview guide.

Remove the container on exit

If you don't want your container to persist after you exit, you should use the --rm flag.

docker run -it --rm ubuntu /bin/bash

A sample use-case: TensorFlow

Here, you can see how you'd use a Docker container to run TensorFlow without having to install dependencies on your local machine.

JUNE 30, 2021

I get this error after running docker run --rm -it IMAGE_TAG.

The problem was that the image I was using wasn't really an image to execute in Docker but a set of steps to build a Python wheel package (.whl).

The solution was to only build the image specifying an --output directory to which the resulting wheel file could be copied.

DOCKER_BUILDKIT=1 docker build --output folder_to_save_wheel .

JANUARY 20, 2021

Supposing you've started your container with ./docker-wine wine notepad and saved your files to your volume, for instance, at My Music folder with the new.txt name.

docker cp wine:/home/wineuser/new.txt ~/Desktop/

Want to see older publications? Visit the archive.

Listen to Getting Simple .