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.
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
<code># 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"]</code>Use a
.dockerignorefile to exclude files that should not be packaged.
Base image is
csphere/php-fpm:5.4.
The
ONBUILDinstruction copies the code into the web root.
init.shconfigures the WordPress MySQL connection, so the database is ready when the container starts.
Build the image:
<code>docker build -t csphere/wordpress:4.2 .</code>List local images:
<code>docker images</code>Find the host IP address:
<code>ifconfig eth0</code>Run the WordPress container:
<code>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</code> -druns the container in the background.
-p 80:80maps host port 80 to container port 80.
--name wordpressnames the container.
-e WORDPRESS_DB_HOST=192.168.1.20sets the database host IP.
-e WORDPRESS_DB_USER=adminsets the database user.
-e WORDPRESS_DB_PASSWORD=csphere2015sets the database password.
The image
csphere/wordpress:4.2creates the container.
Access
http://your_ip, choose a language, and complete the WordPress setup.
ENTRYPOINT vs CMD
ENTRYPOINT configures a container to run as an executable. Preferred exec form:
<code>ENTRYPOINT ["executable", "param1", "param2"]</code>Shell form:
<code>ENTRYPOINT command param1 param2</code>CMD provides default parameters to
ENTRYPOINTor runs a command. Preferred exec form:
<code>CMD ["executable", "param1", "param2"]</code>It can also supply parameters to
ENTRYPOINT:
<code>CMD ["param1", "param2"]</code>Or use the shell form:
<code>CMD command param1 param2</code>Practical test of CMD
<code>vim Dockerfile
FROM centos:centos7.1.1503
CMD ["/bin/echo", "This is test cmd"]</code> <code>docker build -t csphere/cmd:0.1 .
docker run -it --rm csphere/cmd:0.1
# Output: This is test cmd</code>Providing a new command at
docker runoverrides the CMD:
<code>docker run -it csphere/cmd:0.1 /bin/bash
# You get a shell</code>Practical test of ENTRYPOINT
<code>FROM centos:centos7.1.1503
ENTRYPOINT ["/bin/echo", "This is test entrypoint"]</code> <code>docker build -t csphere/ent:0.1 .
docker run -it csphere/ent:0.1
# Output: This is test entrypoint</code>By default, ENTRYPOINT cannot be overridden, but it can be replaced using the
--entrypointoption:
<code>docker run -it --entrypoint=/bin/bash csphere/ent:0.1
# You get a shell</code>This demonstrates that ENTRYPOINT is fixed unless explicitly changed at run time, while CMD is more flexible.
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.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.