Start a Docker Container Without Specifying a Command

05-12-2021

I apologize, the title of the post is slightly misleading, we will be specifying a command, but it isn’t the command you’re normally using to start the container.

If you need to do some troubleshooting of a Docker container and the entrypoint or startup command you defined might be the problem then you need a way to start a container and keep it running so that you can log into the container and do some investigating. For example you have defined a command, in your Dockerfile or docker-compose.yml to start a NodeJS process, CMD ["npm", "run", "start"]. However, there is an issue and the container fails to start, you could access the logs, but that can be time consuming and frustrating.

By replacing the command to start your process (web server, etc.) with CMD ["sleep", "3000"]. This will start the container and cause it to sleep after a period of time. This now gives you a container you can access to investigate, test commands, etc.

Dockerfile

1
2
3
# ...

CMD ["sleep", "3000"]

docker-compose.yml

1
2
# ...
command: ["sleep", "3000"]

Log into container

1
docker exec -it <container name> bash