Build and run container from Dockerfile

Step 1 – Dockerfile

FROM debian:jessie
MAINTAINER user@server.com
RUN apt-get update
RUN apt-get install -y vim
RUN apt-get install -y apache2
RUN DEBIAN_FRONTEND=noninteractive apt-get install -y mysql-server
RUN apt-get install -y php5
RUN apt-get install php5-mysql
RUN apt-get autoremove
 
ENV APACHE_RUN_USER www-data
ENV APACHE_RUN_GROUP www-data
ENV APACHE_LOG_DIR /var/log/apache2
ENV APACHE_LOCK_DIR /var/lock/apache2
ENV APACHE_PID_FILE /var/run/apache2.pid
 
EXPOSE 80
# Launch all services
# CMD ["apache2ctl", "-D", "FOREGROUND"]
COPY startup.sh /
RUN chmod 777 /startup.sh
CMD ["bash","/startup.sh"]

Step 2 – Create startup.sh

cat > startup.sh
#!/bin/bash
 
apache2ctl -D FOREGROUND

Step 3 – Building image

docker build -t aghayevcomdockerfile:latest .

Step 4 – Check if image was created

REPOSITORY             TAG                 IMAGE ID            CREATED             SIZE
aghayevcomdockerfile   latest          19e27a7ca24b        11 minutes ago      389MB
debian                 jessie              5d7d9c6338e8        3 weeks ago         129MB

Step 5 – Creating container

docker run -d -t -p 80:80 -p 443:443 -p 3306:3306 -v /home/user/Projects:/home -v /home/user/Servers:/opt --name aghayevcom aghayevcomdockerfile:latest

Step 6 – Check if container created

CONTAINER ID        IMAGE                             COMMAND                  CREATED             STATUS              PORTS                NAMES
8946f43f47ea        aghayevcomdockerfile:version1.0   "bash /startup.sh"   2 minutes ago       Up 2 minutes        0.0.0.0:80->80/tcp   aghayevcom
 
p.s. Pay attention to COMMAND column, it should show which command will run first when u start container. In case of direct call of apache2ctl it shows this
#850703e85940        aghayevcomdockerfile:version1.0   "apache2ctl -D FOREG…"   2 minutes ago       Up 2 minutes        0.0.0.0:80->80/tcp   aghayevcom

Step 6 – Run container

docker start aghayevcom
 
p.s. It is possible to start container by direct calling Container ID
# docker start 8946f43f47ea

Leave a Reply