"docker attach" vs "docker start -ai" for a running container

Viewed 164

The docker document says that docker attach is used to attach to a running container (https://docs.docker.com/engine/reference/commandline/attach/) and docker start is used to start stopped containers (https://docs.docker.com/engine/reference/commandline/start/).

However, I tried applying docker start -ai to a running container, and it looks that it can successfully attach to the running container. So my question is "Are docker start -ai and docker attach the same when they are used to attach to a running container?".

1 Answers

See the code of docker start:

Ln99:  resp, errAttach := dockerCli.Client().ContainerAttach(ctx, c.ID, options)
Ln136: dockerCli.Client().ContainerStart(ctx, c.ID, startOptions)

docker start consists separate attach & start operation, if the container already start, just skip this start operation, but attach still works there.

So, they are same in this scenario. BTW, from October 2014, docker team suggest to use docker exec to enter the container.

Related