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
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
1234, Docker forwards the communication to port
3000of the container
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
code_b into our image.
code_a as our
docker build -t my_image_a --build-arg code_dir=code_a . docker run -it my_image_a # Prints 'This is code_a!'
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
# 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!
For a Docker container started with the
docker run command to be removed when it exits, you need to use the
docker run -it --rm ubuntu /bin/bash
"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
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
Here's a summary from Docker's docs.
When you run this command, the following happens (assuming you are using the default registry configuration):
docker pull ubuntu).
docker container createcommand manually).
/bin/bash. Because the container is running interactively and attached to your terminal (due to the
-tflags), 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.
If you don't want your container to persist after you
exit, you should use the
docker run -it --rm ubuntu /bin/bash
Here, you can see how you'd use a Docker container to run TensorFlow without having to install dependencies on your local machine.
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 (
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 .
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
docker cp wine:/home/wineuser/new.txt ~/Desktop/