Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

docker start a container after stopping

Tags:

docker

I built a phalcon php image with this Dockerfile

FROM ubuntu:14.04

MAINTAINER betojulio

COPY apache2_evogas.conf /tmp/apache2.conf

RUN apt-get update && apt-get install -y \
    apache2 \
    php5-dev \
    php5-mysql \
    libapache2-mod-php5 \
    gcc \
    libpcre3-dev \
    git \
    && rm -rf /var/lib/apt/lists/* \
    && git clone git://github.com/phalcon/cphalcon.git \
    && cd cphalcon/build \
    && ./install \
    && cd /etc/php5/apache2/conf.d \
    && echo 'extension=phalcon.so' > phalcon_php.ini \
    && cp /tmp/apache2.conf /etc/apache2/apache2.conf \
    && a2enmod rewrite

CMD /usr/sbin/apache2ctl -D FOREGROUND

and I create the container running this command line

docker run -d -p 80:80 --name webserver -v /www:/var/www/html  betojulio/phalcon_project:1.0

and everything works well.

My question is, after I stop the container how do I restart the same container 'webserver' with the same options '-d -p 80:80 -v /www:/var/www/html' or after stopping the container I have to delete it and create it again?

Thank you in advance.

like image 297
julio alberto leal rivera Avatar asked Apr 29 '16 20:04

julio alberto leal rivera


People also ask

How do I restart a container in Docker?

Use a restart policy. To configure the restart policy for a container, use the --restart flag when using the docker run command. The value of the --restart flag can be any of the following: The following example starts a Redis container and configures it to always restart unless it is explicitly stopped or Docker is restarted.

What happens when you stop a docker container?

If you manually stop a container, its restart policy is ignored until the Docker daemon restarts or the container is manually restarted. This is another attempt to prevent a restart loop. Restart policies only apply to containers. Restart policies for swarm services are configured differently.

How to run a docker container all the time?

So if want to have your container running all the time then there should be a foreground process running in your docker. For testing if you run docker run <imageid> echo hi it will return the output means your container is fine.

Should I restart the container if it stops?

Always restart the container if it stops. If it is manually stopped, it is restarted only when Docker daemon restarts or the container itself is manually restarted. (See the second bullet listed in restart policy details)


2 Answers

Like the other answer said, to start a stopped container, you run:

docker start <container name or id>

If it doesn't stay running, check the logs:

docker logs <container name or id>

If you want the container to restart automatically if it stops for some reason look at docker restart policies.

docker run -d --restart=unless-stopped -p 80:80 --name webserver -v /www:/var/www/html  betojulio/phalcon_project:1.0

Also, you might want to change your CMD to the following in your Dockerfile.

CMD ["apache2", "-D", "FOREGROUND"]
like image 97
Ken Cochrane Avatar answered Oct 21 '22 09:10

Ken Cochrane


docker start webserver

That's it.

like image 44
Robert Moskal Avatar answered Oct 21 '22 07:10

Robert Moskal