Use docker build
to create an image from a Dockerfile.
Use docker run
to create and start a new container from an image.
Use docker start
with -a
to restart a container and view its output.
Use docker start
and docker logs
to restart a container and view its logs separately.
To build a Docker image from a Dockerfile, use the following command. This will create an image tagged as cowsayapp
from the Dockerfile in the current directory.
docker build -t cowsayapp .
Once the image is built, you can run a container from this image. This command will start a new container instance from the cowsayapp image.
docker run --name my-cowsay-container cowsayapp
This creates and starts a container named my-cowsay-container. If your container is set up to display output (like using the cowsay command), this output will be shown in the terminal.
After running a container, it will eventually stop (especially if its main task completes). To restart the same container without creating a new one, you can use the docker start command.
Option 1: Attach to the Container's Output To restart the container and view its output, use:
docker start -a my-cowsay-container
This restarts the container and attaches your terminal to its output, allowing you to see the output from the container's main process.
Option 2: Viewing Container's Logs Alternatively, you can start the container and view its logs:
docker logs my-cowsay-container
This will start the container in the background and docker logs will display the output of the container.
To start a container and not have its output, or if the container runs and you don't want it to hanf in the shell. Just start it without the -a
flag.
docker start container-name
To stop the container if it is running do.
docker stop container-name
The -a
or --attach
flag attaches your terminal to the container's output, useful for viewing output or interacting with the container.
The -d
or --detach
flag starts the container in detached mode, where it runs in the background and your terminal is free for other tasks.
The -a
and -d
flags in Docker commands serve different purposes, and understanding them will clarify how your containers behave when you run them:
Flag: -a
or --attach
Usage: This flag is used with docker start or docker run commands.
Function: It attaches your terminal's standard input, output, and error (STDIN, STDOUT, STDERR) to the container.
Effect: When used with docker start, it allows you to see the output of the container in your terminal. For docker run, it can be used to keep the terminal attached to the container even after the command to start the container has been issued.
Scenario: You would use -a when you want to interact with the container or view its output directly in your terminal.
Flag: -d
or --detach
Usage: This flag is used with the docker run
command.
Function: It starts the container in detached mode.
Effect: The container runs in the background and does not occupy the current terminal session. You won't see any immediate output from the container in your terminal.
Scenario: You would use -d
when you don’t need to interact with the container immediately and want your terminal free for other tasks.
Running a Container in Attached Mode:
docker run -a my-container
This command will show you the output of the container in your terminal.
Running a Container in Detached Mode:
docker run -d my-container
This command will start the container in the background, and you'll get the command prompt back immediately for other uses.
In summary, use -a when you want to see or interact with the container's output directly in your terminal, and use -d when you want the container to run in the background without tying up your terminal.
docker ps
This command shows all currently running containers, including their names, IDs, and other details, note if a container is one off and starts and exits after a CMD was run, it won't show as running.
docker ps -a
This command lists all containers, whether they are running or have been stopped. You can find the names in the output.
docker inspect <container-id>
Replace with the actual container ID.
If the Docker container's main process (specified in the CMD or ENTRYPOINT of the Dockerfile) completes, the container stops. For your cowsayapp, this happens immediately after displaying the message.
While a container's main process is active, the container is considered "running", and you can interact with it.
Run the container in interactive mode with a shell. This allows you to execute commands inside the container. For example, to start a Bash shell in an Ubuntu-based container:
Check where your local shell that you want to use is installed, for example if using Git Bash on a windows, do which bash
here the ouput is usr/bin/bash
docker run -it --name my-temp-container cowsayapp usr/bin/bash
This opens a Bash shell inside the container. You can run commands, explore the filesystem, or perform other tasks. The -it
flags are for interactive (-i
) and terminal (-t
) mode. Remember run, is for the first time you run the container. This will give you a interactive shell everytime the container starts. So whenever you do docker start containerappname
If you just need temporary access to the container's environment, you can run a one-off new container with an interactive shell using the same image.
docker run -it --rm cowsayapp usr/bin/bash
Note, you run the image name, not the container. Essentialy you run
an image and start
a container. This command starts a new container with an interactive Bash shell. The --rm
flag ensures that the container is removed after you exit the shell, which is useful for temporary or one-off use.
If your container is not running and it is set to run a command and stop and you did not run it with a shell like above, this will NOT work. However, if the container is already running and you have a long-running container, you can execute commands in it using docker exec
. For example:
docker exec -it my-running-container usr/bin/bash
Replace my-running-container with the name or ID of your running container. This command gives you access to a Bash shell inside the container
Containers are often used for single, specific tasks (like running a web server, executing a script, etc.). In these cases, the container stops after completing its task.
For exploration, debugging, or development, running a container in interactive mode with access to a shell is common.
For services (like databases, web servers, etc.), containers are run in the background and managed as needed.
Using a bind mount in Docker allows you to mount a specific file or directory from the host machine into the container. This is particularly useful for development purposes, as it allows you to work on your code in the host environment and see the changes reflected in the container in real-time.
-
Choose the Directory to Mount:
- Identify the directory on your host machine that you want to mount into the container.
-
Run the Container with a Bind Mount:
-
Use the
docker run
command with the-v
or--mount
flag to specify the bind mount. -
The general format is:
docker run -v <host_directory>:<container_directory> ...
or
docker run --mount type=bind,source=<host_directory>,target=<container_directory> ...
-
For Windows Git Bash:
docker run -v /c/Users/Username/path:/containerPath -it imageName
-
For Windows WSL:
docker run -v /mnt/c/Users/Username/path:/containerPath -it imageName
-
This command mounts the directory from your host machine to the specified path in the container.
-
-
Use the
-it
Flag for Interactive Mode:-
The
-it
flags are combined to make the Docker container start in interactive mode with a TTY, so you can interact with the container through the command line. -
-i
or--interactive
keeps the standard input open even if not attached. -
-t
or--tty
allocates a pseudo-TTY. -
Example Command:
docker run -it -v /path/on/host:/path/in/container imageName
-
- Overwriting Data: The directory you mount into the container will overwrite the contents of the directory in the container at the specified path. Ensure that you are not overwriting any important data within the container.
- Path Differences: Be aware of the path differences in Git Bash and WSL. Use the appropriate path format for your environment.
- Permissions: Ensure that you have the necessary read/write permissions for the directories you are mounting.
Docker volumes are used to persist data and share it between the host and containers. Volumes are managed by Docker and are more robust than bind mounts.
-
Create a Docker Volume:
docker volume create my_volume
-
Attach the Volume to a Container:
docker run -v my_volume:/path/in/container -it containerName
docker cp /path/on/host/file host_container:/path/in/container
docker cp container:/path/in/container/file /path/on/host
If your container has network access and the necessary tools (like curl, wget, or scp), you can download or upload files directly inside the container.
- Isolation and Security: Be cautious about what you mount into a container, as it can potentially compromise the security and isolation provided by Docker.
- Data Persistence: Data inside a container's filesystem is ephemeral and will be lost when the container is removed unless it is stored in a volume or a bind mount.
- Use bind mounts for development and real-time code testing.
- Use Docker volumes for data that needs to persist beyond the life of a container.
- Be mindful of the paths and permissions when working with file mounts in Docker.
- This works in windows git bash, to
bind
a file path.
docker run -v C:/Users/Victor/hello/:/victor -it ubuntu usr/bin/bash
- This works in windows wsl Ubuntu, to
bind
a file path.
docker run -v /mnt/c/Users/Victor/hello:/victor -it ubuntu usr/bin/bash
It was haning and not working, thus I had to reset WSL with command wsl --shutdown
executed on powershell. I also closed and reopend the docker service.
docker run -it ubuntu usr/bin/bash
This will download an ubuntu image and create a container and drop you into the shell of that container. Note the name of the container is random. If you want to name it, add the --name
flag.