Docker attach vs exec & When to Use What

Docker attach vs exec & When to Use What

Hello lovely people! πŸ‘‹ During my day to day work with SONiC I have to launch new Docker containers and run commands within those containers. I usually have an ENTRYPOINT or CMD defined in my Dockerfile which means that there is always a default process that starts when I run my Docker images. When I started working with Docker I would always use the attach command to connect to a running container.

Hello lovely people! πŸ‘‹ During my day to day work with SONiC I have to launch new Docker containers and run commands within those containers. I usually have an ENTRYPOINT or CMD defined in my Dockerfile which means that there is always a default process that starts when I run my Docker images. When I started working with Docker I would always use the attach command to connect to a running container.

As an example, my Dockerfile usually looks something like this:

FROM ubuntu:18.04

RUN apt-get update
RUN apt-get install -qy vim

COPY startup.py /startup.py
ENTRYPOINT ["python", "startup.py"]

I then build an image based on this Dockerfile:

sudo docker build -t yasoob/imagename 

Then I usually start up a container based on this latest image:

sudo docker run --name container_name -it -d yasoob/imagename:latest

At this point, I have two ways to connect to the Docker container. I can either use attach or exec. It is important to know the difference.

  • attach allows you to connect and interact with a container’s main process which has PID 1
  • exec allows you to create a new process in the container

docker attach exec images entrypoint dockerfile

Bootstrap 5 Complete Course with Examples

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Building a simple Applications with Vue 3

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

Reverse Engineer Docker Images into Dockerfiles

In this article, we will show how it's possible to reconstruct a Dockerfile from an image using two tools, Dedockify, a customized Python script provided for this article, and dive. The basic process flow used will be as follows.

Docker creating an image | Build and run your image | Docker Fundamentals

Docker create an image from a container | Creating your own image | How to create Docker Image from a Container and Dockerfile

Docker manifest - A peek into image's manifest.json files

The docker manifest command does not work independently to perform any action. In order to work with the docker manifest or manifest list, we use sub-commands along with it. This manifest sub-command can enable us to interact with the image manifests. Furthermore, it also gives information about the OS and the architecture, that a particular image was built for. The image manifest provides a configuration and a set of layers for a container image. This is an experimenta

What is Dockerfile, Docker Image, and Docker Container

It all starts with Dockerfile, a text file with a few lines of instructions. This tutorial explains the terms Dockerfile, Docker Image, Docker Container, and Docker repository

Docker Explained: Docker Architecture | Docker Registries

Following the second video about Docker basics, in this video, I explain Docker architecture and explain the different building blocks of the docker engine; docker client, API, Docker Daemon. I also explain what a docker registry is and I finish the video with a demo explaining and illustrating how to use Docker hub.