- Ansible beginner tutorial - Wed, Nov 15 2023
- Nested Docker containers: Run Docker in a Docker container - Fri, Nov 10 2023
- Docker networking: Connect a Docker container - Fri, Oct 27 2023
Prerequisites
- A Linux system with Docker installed and running
- A root user or a user account with sudo privileges
Create an NGINX test container
Before starting, you will need a running container to use the docker exec command. Here, we will create an NGINX Docker container to demonstrate the tutorial.
docker run -dit --name nginx-container nginx:latest
This command will download the latest NGINX image from the Docker Hub registry and start an NGINX container in the background.
A brief explanation of the above command is provided below:
- run: Create a container.
- -d: Start a container in the background.
- -it: Run a container in interactive mode.
- --name: Define the name of the container.
After you create an NGINX container, you can verify the running container using the following command:
docker ps
You will see the container status in the following output:
Basic syntax of the docker exec command
Before working with the docker exec command, you must be familiar with all the options that you can use with it.
The basic syntax for the docker exec command is as follows:
docker exec [OPTIONS] CONTAINER COMMAND [ARG...]
A brief explanation of some common options is provided below.
- -i: Keep STDIN open, even if not attached.
- -t: Allocate a pseudo-TTY.
- --user: Specify the username to use when running a command inside the container.
- --workdir: Define the working directory inside the container.
- CONTAINER: Specify the name of the container that you want to connect to.
- COMMAND: Specify the command that you want to run inside the container.
- ARG: Specify optional arguments to pass to the command being executed.
Run commands in an interactive shell
Interactive mode allows you to open a bash shell inside the container. This way, you can run multiple commands and interact with the container's environment.
The basic syntax for running a command in an interactive shell is shown below:
docker exec -it container-name /bin/bash
Let's open an interactive shell inside an NGINX container:
docker exec -it nginx-container /bin/bash
Once you are connected to the NGINX container, you will get the following shell:
root@069a2ecdf40a:/#
The number after @ is the unique identifier (ID) of your Nginx Docker container. It is a random string of hexadecimal characters that is generated when you create the container.
You can now browse the container's file system and run any command inside the container. For example, run the following command to see the container's file system.
ls /
You will see the container's file system in the following output:
You can exit from the NGINX container using the following command:
exit
Run commands in noninteractive mode
In noninteractive mode, you can run a command inside the specified container without connecting to it. The basic syntax for running a command in noninteractive mode is shown below:
docker exec [container-name] [command]
For example, run the "nginx -v" command inside the NGINX container.
docker exec nginx-container nginx -v
This will retrieve the NGINX version from the NGINX container and display it in your terminal.
nginx version: nginx/1.25.2
To check the disk usage of the NGINX container, run the df -h command inside the container.
docker exec nginx-container df -h
This will run the df -h command inside the NGINX container and display the output:
Run commands as a different user in a container
When you run any command inside the container, it will run as the root user. In this case, add the --user flag to specify a different user to run a command.
The basic syntax for running a command as a different user in a container is shown below:
docker exec --user [username] [container-name] [command]
Let's run the whoami command as the www-data user inside the container.
docker exec --user www-data nginx-container whoami
If the command is executed successfully, you will receive the following output:
www-data
Run commands with environment variables
You can also run the docker exec command with specific environment variables.
For example, run the docker exec command inside the container with a custom environment variable:
docker exec -e NEW_VAR='my_variable' nginx-container env
You will see your newly defined environment variable on the following screen:
You can also pass multiple environment variables stored in a file using the --env-file flag. Let's create a file named env.txt.
nano env.txt
Add the following variables:
USER=hitesh PASSWORD=my-password VAR=dev
Save and close the file, then run the docker exec command followed by your file name.
docker exec --env-file env.txt nginx-container env
You will see all of your environment variables on the following screen:
SSH into a running container
You can also connect to a Docker container using SSH. In this section, we will show you how to enable SSH on the running container and connect it using the SSH protocol.
Before starting, you will need to enable the SSH service in the container that you want to connect using SSH.
First, connect to a running container with the following command:
docker exec -it nginx-container /bin/bash
Once connected, install the SSH service and the nano editor inside the container.
apt update -y apt install ssh nano -y
Next, edit the SSH default configuration file and permit root login:
nano /etc/ssh/sshd_config
Add the following line:
PermitRootLogin yes
Save and close the file, and then restart the SSH service with the following command:
/etc/init.d/ssh restart
Next, set the root password with the following command:
passwd root
Set your password as shown below:
Next, exit the running container with the following command:
exit
You will need to find the IP address of the container that you want to connect with SSH. You can use the docker inspect command to get the IP address of the container:
docker inspect -f "{{ .NetworkSettings.IPAddress }}" nginx-container
You should get the IP address that has been automatically assigned by the built-in DHCP server:
172.17.0.2
Now, run the ssh command to connect to a running container:
ssh root@172.17.0.2
Since you're connecting for the first time, you will be asked to accept the fingerprint of the remote system (your Docker container). Provide the password of the root user we set earlier, and hit Enter. You should get into the NGINX container, as shown below:
Conclusion
In this tutorial, we explained different methods for running commands inside the running container. We also showed you how to connect to a running container via SSH. The abovementioned techniques allow you to easily manage and interact with your Docker containers. Read the Docker exec official documentation page for more information.
Read the latest IT news and community updates!
Join our IT community and read articles without ads!
Do you want to write for 4sysops? We are looking for new authors.