Docker

Docker Attach VS Exec

The Docker platform is one of the famous development and deployment solutions that build, run, and share software, programs, and apps. Docker is accessible in both CLI and GUI versions. However, Docker CLI has more control over the Docker components as it offers numerous commands to manage and process these components.

This post will demonstrate the “docker attach” vs “docker exec” commands.

Docker “attach” VS Docker “exec”

The “docker attach” and “docker exec” commands are used to run the container’s terminal. However, these two commands are not similar to each other in any way. The “docker attach” command attaches to the executing container. It is used to view the containers stdout, stdin, and stderr in the terminal. In contrast, the “docker exec” is utilized to execute the external command and extra things within an executing container.

The “docker attach” command can use only one instance of shell and in order to execute another instance or terminal, you are required to run the “docker exec” command.

How to Use “docker attach” in Docker?

The “docker attach” command attaches to the running container and shows the output, standard input, and standard errors of containers. This command is utilized to execute the process shell. To use the “docker attach” command, look at the provided instructions.

Step 1: Run Container

First, execute the container using the “docker run” command. For instance, we have executed the container using the following options:

  • -i” option runs the container interactively.
  • -t” allocates the container with the TTY terminal.
  • ubuntu:latest” is a Docker image utilized to run the container:
docker run -it --name test-cont ubuntu:latest

 

Open another terminal and run the “docker attach” command to attach the container’s stdin, stdout, and stderr:

docker attach test-cont

 

Now, users can view the container’s log and output. For instance, we have executed “ls” to list the elements of the container:

ls

 

How to Use “docker exec” in Docker?

The Docker exec command runs the external commands and other things like files within the Docker containers. The “docker exec” command is used to execute the multiple instances of the container shell or to start a new container instance within an executing container, and to debug the errors. To utilize the “docker exec” command, follow the mentioned steps.

Step 1: Run Docker Container

Start the container by executing the Docker image with the following options:

  • –name” is utilized to assign the name to the container.
  • -d” runs the containers as a backend service.
  • -p” allocates the container’s exposed port as “80:80”.
  • html-image” is our custom image to run the container:
docker run --name html-container -d -p 80:80 html-image

 

Step 2: Run Command Inside the Container

Next, execute the “docker exec <container-name> sh” command to execute the container’s shell:

docker exec -it html-container sh

 

After that, run any external command to within the container. For illustration, we have executed the “echo” command:

echo "Hello! Welcome to Linux Hint"

 

We have elaborated on the difference between “attach” and “exec” commands in Docker.

Conclusion

The “docker attach” and “docker exec” commands are not the same in any way except for opening the container terminal. The “docker attach” command attaches the stdin, stdout, and stderr to containers. In contrast, “docker exec” runs the external command and extra things within the container. This post has demonstrated the primary difference between “attach” and “exec” commands in Docker.

About the author

Rafia Zafar

I am graduated in computer science. I am a junior technical author here and passionate about Programming and learning new technologies. I have worked in JAVA, HTML 5, CSS3, Bootstrap, and PHP.