Docker is an open-source containerization tool, which allows users to create and manage software containers. This article gives you a rundown of how to do some common tasks with Docker including: installing the command line interface for accessing your Dockers, putting in a password for ssh access (for remote use), creating images from scratch or pulling existing ones from docker hub.
The “docker run command in specific directory” is a command that allows you to run commands inside of a container. It is useful for debugging or running commands that need privileged access.
Have you ever needed to see the contents of a Docker container? Containers are designed to be idempotent: if one fails, a new one is deployed. Life isn’t always that straightforward. To diagnose the problem, you must execute commands in the container. The docker exec command may assist with this.
This article will show you how to use the docker exec command to perform commands on a running Docker container.
Prerequisites
You must follow the guidelines below to follow along with the examples in this tutorial.
- On Windows, Linux, or macOS, any current version of the Docker Desktop will work. This tutorial makes use of v3.1.0 on Windows 10.
Docker for Windows: How to Deploy Your First Container
Getting an NGINX Container Started
Docker exec is a command execution tool for containers. However, you’ll need a container to execute those instructions in first. To begin, download a Docker image and create a demonstration container.
- To house the files for the container, create a new directory (this example uses C:gitrepostest).
2. Write the following code in a file called dockerfile (no extension). The Dockerfile specifies the actions that must be followed to construct a container.
COPY index.html /usr/share/nginx/html/index.html FROM nginx:alpine
3. Create an index.html file in the same directory that includes the following code. This is an HTML file that displays a Hello World message when the container is launched.
<!DOCTYPE html> <html lang=”en”> <head> <meta charset=”UTF-8″> <meta name=”viewport” content=”width=device-width, initial-scale=1.0″> <meta http-equiv=”X-UA-Compatible” content=”ie=edge”> <title>Hello World – Nginx Docker</title> <style> h1{ font-weight:lighter; font-family: Arial, Helvetica, sans-serif; } </style> </head> <body> <h1> Hello World </h1> </body> </html>
4. Create a Docker container for Nginx. Because the Dockerfile is located in the current working directory, use. to tell the Docker engine to search there. Also, use the t argument to tag the container with my-ngnix to make it simpler to find in the future.
my-nginx docker build -t
The build command is used to create a Docker container.
5. Now that the container has been constructed, use the Docker run command to start it.
docker run —rm -d -p 80:80 my-nginx # rm -d -p 80:80 my-nginx # d – Return control of the command-line once the command has been executed # p – Map the internal container port 80 to an external port 80 docker run —rm -d -p 80:80 my-nginx
The Docker run command is used to start the container.
6. Finally, open your web browser and go to http://localhost/ to view what you’ve created.
The output of the NGINX container in use.
Using Docker Exec to Execute Commands
You may need to perform a command interactively while executing commands in a Docker container. Typing in a command, receiving response, typing in another command, and so on are all examples of interactive command execution. Interactive instructions take over your session and make it impossible for you to accomplish anything else.
But what if you already know the commands you want to send to the container and want to perform them in the background? You may then execute non-interactive instructions. Non-interactive commands enable you to submit a command to Docker and have control of the console returned immediately.
Finding the Container ID and Name
You may now run commands within the container once it has been constructed. Locate the NGINX container’s name or ID before performing a command. In Docker commands, either the name or the ID will work. With that in mind, memorizing the ID can be more difficult than remembering the name!
Run the Docker ps command to get the following information about any currently running containers.
Docker containers that are currently running.
To use later, copy either the unique ID, e17e4b6be01a, or the randomly created name, mystifying chandrasekhar.
Using Docker Exec to Execute a Non-Interactive Command
Copy and execute the following command to get a list of files in the /var/log directory using the ls -l command as an example of a non-interactive command. To the Docker exec command, add everything after the container name, mystifying chandrasekhar.
ls -l /var/log docker exec mystifying chandrasekhar
Using the NGINX container to run a directory listing.
Using Docker Commands to Avoid Console Output
Large actions avoid clogging the console by returning shell control to the user immediately. The disconnected d option disables console output. The program below uses the touch command inside the container to create the file /tmp/execWorks and does not print anything to the terminal.
touch /tmp/execWorks docker exec -d mystifying chandrasekhar
Using Docker Exec to Run Interactive Commands
You’ve learnt how to use docker exec to execute non-interactive commands in a Docker container up to this point. However, you may need to troubleshoot a container at times, such as when you need to provide instructions to the container interactively. In such situation, interactive instructions are required.
When using docker exec interactively, there are two options: I and t. The I option maintains STDIN open, enabling instructions to be delivered to the container, while the t option assigns a pseudo-TTY (PTY) to input commands into.
As shown by the prompt change to / #, copy and paste the following command to open an interactive command prompt to the running Docker container using the Bourne (sh) shell.
mystifying chandrasekhar sh docker exec
An interactive Docker shell is being run.
Run the instructions below to list files from inside the container once you’re in the shell. Finally, quit the interactive shell using the exit command.
To the container, open an interactive command prompt.
Pass the path to the w option directing Docker to start the shell in a particular directory to open an interactive prompt in that directory.
Environmental Variables are Passed to a Running Container
To specify parameters at launch, many applications employ environment variables. Most Java programs, for example, require that the JAVA HOME environmental variable be set to the Java path.
You can variables from the environment to a session using the e option. For example, perhaps you need to populate an environment variables called MYVAR into a running container. To do that, use the e option and provide the key/value pair of MYVAR=”<some value>” as shown below.
mystifying chandrasekhar sh echo docker exec -it -e MYVAR=”hello” $MYVAR
variables from the environment
Using a File to Pass Environment Variables
Keeping environmental variables in a file may be simpler if you have a lot of them or a common setup. The —env-file option allows you to provide the file to Docker as a relative or absolute location. This method is often used to supply a container with secure credentials. Never put your passwords inside version control!
Make an env-vars.txt text file containing the environmental variables to send and their values. This file does not need the.txt file extension and may be named anything you like.
Environmental variables are specified in a text file.
The env-file option is used to provide environmental variables to Docker. Use the echo command to see whether the variables are accessible, as seen in the picture below.
# Open an interactive prompt and provide the env-vars.txt file. mystifying chandrasekhar sh docker exec -it —env-file env-vars.txt # Check if the Docker container’s environmental variables are accessible echo echo $MYVAR echo $FOO $SOMETHING
Passing a file containing environmental variables to Docker and confirming that the variables are present in the container.
As a Different User, Interacting with a Running Container
Applications are often launched as a particular user in production to limit access. If you run apps as a certain user in production, you should do the same while testing commands.
The Docker container is started as the nginx user in this example. To notify Docker to start the container as the nginx account, provide the user to the w option. The whoami command, which is executed from inside the container, ensures that the nginx user is in fact active.
mystifying chandrasekhar sh whoami docker exec -it -u nginx
As the nginx user, start the container.
Steps to Follow
Using the docker exec command, you’ve learnt how to run commands inside a running container. You now have a powerful new tool to debug Docker containers by using the exec command to enter and interrogate running containers.
Instead of transferring a single file, consider taking what you’ve learned a step further and using Git version control to bring a static website into the container. If you’re unfamiliar with Git, the article A Beginner’s Guide to Visual Studio Code and Git is a good place to start.
The “docker exec command” is a command-line tool that allows users to run commands in Docker. This can be helpful when you are trying to troubleshoot or debug issues with the container.
Related Tags
- docker run command in container
- docker exec -it bash example
- docker run bash in container
- docker exec multiple commands
- docker exec stdout