Ad space (banner)
AD

Got the fundamentals? A structured Docker course is a fast way to level up.

Find Docker courses on Udemy

Affiliate link. Costs you nothing extra.

🐳Docker Lessons
Lesson 31 / 45

Setting the Startup Command with CMD

When you want to define what the container does when it starts, use CMD. It sets the default command executed at container start.

The syntax is CMD ["executable", "arg"]. Only the last CMD in a Dockerfile takes effect.

Writing CMD ["node", "app.js"] means the container runs your Node application on start, unless the caller overrides it.

A common stumble is writing several CMD instructions and wondering why only one runs. To run multiple things, use a startup script.

In real work, CMD defines the one main process of the container — and when that process exits, the container stops.

Docker
Try it (type this into the pseudo terminal)
docker build -t myapp .

🧑‍💻 You can type this command into the Docker pseudo terminal to try it yourself (this page itself has no interactive terminal).

Ad space (banner)
Ad space (in-article)