Cloud Native 9 min read

Master Building Docker WordPress Images and Understanding ENTRYPOINT vs CMD

This guide walks you through preparing Docker, building a csphere/wordpress:4.2 image, running the container with proper environment variables, and clarifies the differences between ENTRYPOINT and CMD through hands‑on examples, helping you master container image creation and command execution in Docker.

Efficient Ops
Efficient Ops
Efficient Ops
Master Building Docker WordPress Images and Understanding ENTRYPOINT vs CMD

Docker Practical Preparation

Four steps: log in to OSChina Git, fork the docker‑training repository, connect to the server via SSH, and clone the repository.

Build csphere/wordpress:4.2 Image

# cd docker-training/wordpress/
# ls -a
.              license.txt           wp-config-sample.php  wp-login.php
..             readme.html           wp-content            wp-mail.php
Dockerfile     wp-activate.php       wp-cron.php           wp-settings.php
.dockerignore  wp-admin              wp-includes           wp-signup.php
index.php      wp-blog-header.php    wp-links-opml.php     wp-trackback.php
init.sh        wp-comments-post.php  wp-load.php           xmlrpc.php

# cat Dockerfile 
from csphere/php-fpm:5.4

add init.sh /init.sh

entrypoint ["/init.sh", "/usr/bin/supervisord", "-n", "-c", "/etc/supervisord.conf"]

Use a .dockerignore file to exclude files that should not be packaged.

Base image is csphere/php-fpm:5.4.

The ONBUILD instruction copies the code into the web root. init.sh configures the WordPress MySQL connection, so the database is ready when the container starts.

Build the image: docker build -t csphere/wordpress:4.2 . List local images: docker images Find the host IP address: ifconfig eth0 Run the WordPress container:

docker run -d -p 80:80 --name wordpress -e WORDPRESS_DB_HOST=192.168.1.20 -e WORDPRESS_DB_USER=admin -e WORDPRESS_DB_PASSWORD=csphere2015 csphere/wordpress:4.2
-d

runs the container in the background. -p 80:80 maps host port 80 to container port 80. --name wordpress names the container. -e WORDPRESS_DB_HOST=192.168.1.20 sets the database host IP. -e WORDPRESS_DB_USER=admin sets the database user. -e WORDPRESS_DB_PASSWORD=csphere2015 sets the database password.

The image csphere/wordpress:4.2 creates the container.

Access http://your_ip, choose a language, and complete the WordPress setup.

WordPress setup screenshot
WordPress setup screenshot

ENTRYPOINT vs CMD

ENTRYPOINT configures a container to run as an executable. Preferred exec form: ENTRYPOINT ["executable", "param1", "param2"] Shell form: ENTRYPOINT command param1 param2 CMD provides default parameters to ENTRYPOINT or runs a command. Preferred exec form: CMD ["executable", "param1", "param2"] It can also supply parameters to ENTRYPOINT: CMD ["param1", "param2"] Or use the shell form:

CMD command param1 param2

Practical test of CMD

vim Dockerfile
FROM centos:centos7.1.1503

CMD ["/bin/echo", "This is test cmd"]
docker build -t csphere/cmd:0.1 .

docker run -it --rm csphere/cmd:0.1
# Output: This is test cmd

Providing a new command at docker run overrides the CMD:

docker run -it csphere/cmd:0.1 /bin/bash
# You get a shell

Practical test of ENTRYPOINT

FROM centos:centos7.1.1503

ENTRYPOINT ["/bin/echo", "This is test entrypoint"]
docker build -t csphere/ent:0.1 .

docker run -it csphere/ent:0.1
# Output: This is test entrypoint

By default, ENTRYPOINT cannot be overridden, but it can be replaced using the --entrypoint option:

docker run -it --entrypoint=/bin/bash csphere/ent:0.1
# You get a shell

This demonstrates that ENTRYPOINT is fixed unless explicitly changed at run time, while CMD is more flexible.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

WordPresscmdImage BuildingENTRYPOINT
Efficient Ops
Written by

Efficient Ops

This public account is maintained by Xiaotianguo and friends, regularly publishing widely-read original technical articles. We focus on operations transformation and accompany you throughout your operations career, growing together happily.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.