Docker image with mysql and .war file on ECS - mysql

I am totally new in Docker community and I am trying to create a custom container image with mysql and a .war file inside and to run it on a AWS EC2 instance. I've tried a lot but I cannot figure this out..
To build the container image I rum this
docker build -t <name-of-image> -f Dockerfile
I suppose Docker file content should contain something like
FROM mysql:latest
ENV TARGETD /opt/apache-tomcat-9.0.35
ENV WAR /target/NewWebApp.war
RUN apt-get -y update
RUN apt-get -y upgrade
# Create database
RUN mkdir /usr/sql
#RUN CHMOD 644 /usr/sql
ADD db.sql /usr/sql/db.sql
RUN mysql -h localhost -P 3306 --protocol=tcp -u root start && \
mysql -u root -e < /usr/sql/db.sql
EXPOSE 3306
ADD ${WAR} ${TARGETD}/webapps
And to run(deploy) image I use
docker run -d -p 8080:3306 <name-of-image>:latest
I have already installed Tomcat on 8080
What can I do in order to run this image and to be able to access it through AWS EC2?

Related

Is it possible to restore removed mysql docker container sql data? [duplicate]

I was running mariadb instance on docker windows toolkit. I did a env vaiable change on the mariaDB container using kitematic. Now it has recreated an instance loosing all my database. Is there a way to recover from this ?
Checked if threre are dangling volumes, and there are few
docker volume ls -f dangling=true
Got the data recovered using the dangling volumes.
Approach is as following.
First get the list of dangling volumes.
$ docker volume ls -f dangling=true
DRIVER VOLUME NAME
local 6f79b6329d98379495a284287e32a7a57605483dd7bf7fa19924fb2a98fb1d19
local 47bb077ef6f6df9f56bd30c35eeb45f35e62213d2c50db6f078bfdeeee6698ec
Then mounted it on to a Ubuntu container (so that you can go inside the directory and check what is there, as there is no other way to do this when you are using Docker Tool Box on windows)
$ docker run --name tempContainer1-UBUNTU -v 6f79b6329d98379495a284287e32a7a57605483dd7bf7fa19924fb2a98fb1d19:/var/lib/backup -t -i ubuntu /bin/bash
Then you will be inside the bash of newly created contianer. Go to newly mounted directory and check content
$cd /var/lib/backup
$ls
$aria_log.00000001 aria_log_control ib_buffer_pool ib_logfile0 ib_logfile1 ibdata1 ibtmp1 multi-master.info mysql performance_schema
-- once you are sure directory data is what you require, make a zip file of the folder
$apt-get update
$apt-get install zip
$cd ..
$zip -r backup.zip backup
On another terminal from host copy the content of container backup.zip to host
$docker cp tempContainer1-UBUNTU:/var/lib/backup.zip .
Then create a docker compose file like following and mount the backup folder as data directory. Run this on linux host as this mounting will not work as expected for mysql on windows.
version: "3.2"
services:
mysql:
image: mariadb:10.4.12
restart: always
ports:
- "3306:3306"
command: mysqld --innodb-flush-method=littlesync --innodb-use-native-aio=ON --log_bin=ON
volumes:
- ./backup_data_folder:/var/lib/mysql
environment:
MYSQL_ROOT_PASSWORD: somepassword
TZ: Asia/Singapore
networks:
- frontend
container_name: maria
networks:
frontend:
Start
$docker-compose up
Once it is up, from another terminal go inside newly created container
$docker exec -t -i maria /bin/bash
-- Take dump of all the DBS
$mysqldump -u root -p --all-databases > alldb.sql
Copy content of the dump to host from another terminal from host
$docker cp maria:/alldb.sql .
Now this sql file is a full dump, restore it as usual on your mysql DB or contianer.
mysql -u root -p < alldb.sql
Recently I had to face the same problem for a lost wordpress container and I've followed the instructions from Don. However, as there were lots of dangling volumes, I had to optimize the process. I've managed a way to do it simpler, in the same terminal, resulting in the following steps:
docker volume ls -f dangling=true
DRIVER VOLUME NAME
local 43277666c8bc3da0b585d90952c2303226c92c6c6a561007c0c7ee00b6da817e
local 4fde3ea412e54a1b8b42fce6dae5de5135b9fd12933b020b43bd482cd5fd2225
local 52074ccfd62fb83b8b40cff5f8024215b34f79ad09b630e067ec732f811f798c
...
Then, for each container execute the following instruction, replacing 43277666c8bc3d... with each VOLUME NAME found. This instruction will remove previously maria-restore containers if they exist, create a new one and attach to it:
docker container ls -a -q --filter "name=maria-restore" && docker container rm -f maria-restore; docker run --name maria-restore -v 43277666c8bc3da0b585d90952c2303226c92c6c6a561007c0c7ee00b6da817e:/var/lib/mysql -d mariadb:10.4.12 mysqld --innodb-flush-method=littlesync --innodb-use-native-aio=ON --log_bin=ON && docker exec -it maria-restore bash
If it wasn't a mysql volume, it will fail and exit immediately. If it is a mysql volume, you'll be inside the mariadb container. The database will be already started. You can then connect to the database to see if it is the right one and backup it:
root#8b35c8e2c474:/# mysql -uadmin -p
root#8b35c8e2c474:/# mysqldump -uadmin -p --all-databases > alldb.sql
root#8b35c8e2c474:/# exit
Copy the backed up database:
docker cp mysql-restore:/alldb.sql .
Finally you'll have to clean up the maria-restore container:
docker container ls -a -q --filter "name=maria-restore" && docker container rm -f maria-restore

Dockerfile to install Nginx and MySQL in same image

I want to install nginx and mysql in same image. I start out with a mysql image with the plan to install docker using dockerfile.
Here is my dockerfile:
FROM mysql:latest
ENV MYSQL_ROOT_PASSWORD=HelloWorld \
MYSQL_DATABASE=content
RUN apt update
RUN apt install nginx -y
COPY nginx.conf /etc/nginx/nginx.conf
This starts the mysql db perfectly and nginx also gets installed. Unfortunately, nginx doesn't start. To start nginx I also added another command in the docker file:
CMD service nginx start
After adding this line in the dockerfile, the container closes after creation. What am I doing wrong here?
I am using below command to start container with above image:
docker run -it -p 3306:3306 -p 8080:80 -p 8081:443 --name mycontainer myimage
it's best to run each process in a separate container. but if you wanna do that, you should create a bash file to start MySQL and Nginx. finally, you should use that bash file as the ENTRYPOINT of your image/container

How to pre-configure and prefill official MySQL docker container?

I want to build a new MySQL image based on the official MySQL docker container image. I want to reduce the number of parameters I need to add when running the image. (eg. -e MYSQL_USER, -e MYSQL_DATABASE and even -e MYSQL_ROOT_PASSWORD='rootsecret')
that already includes my settings for the global variables and my Create Database SQL file in the docker-entrypoint-initdb.d folder.
How do I add all my settings and create a new image to simply docker run mysql:config1 docker run mysql:config2 and so on?
You could build your own mysql docker image using a docker file, configure username, password and everything else you might need, build that image, upload it to the docker hub and then when you launch a new docker container you just use the previously built container.
An example of a Docker file to build an ubuntu image with a mysql server inside would be something like bellow (save it to a file called Dockerfile):
FROM ubuntu:latest
RUN apt-get update \
&& apt-get install -y apt-utils \
&& { \
echo debconf debconf/frontend select Noninteractive; \
echo mysql-community-server mysql-community-server/data-dir \
select ''; \
echo mysql-community-server mysql-community-server/root-pass \
password 'Desired-Password'; \
echo mysql-community-server mysql-community-server/re-root-pass \
password 'Desired-Password'; \
echo mysql-community-server mysql-community-server/remove-test-db \
select true; \
} | debconf-set-selections \
&& apt-get install -y mysql-server mysql-client
Then build your mysql docker container like this (you have to be in the folder where the Docker file was/is saved):
docker build my-ubuntu-mysql-docker
Then you have to push it to the docker hub and then you can use it to start a new docker container like this:
docker run -d -p 2222:22 -p 3306:3306 --name my-ubuntu-mysql-docker ...
Where 2222 is local ssh port mapped to ssh port 22 of the docker container and 3306 is local mysql port mapped to the mysql port of the docker container.
I hope this helps!
The following has to be written into the Dockerfile:
FROM mysql:latest
LABEL Name=mylabel Version=0.0.1
COPY path/to/sh/sql/sql.gz/files /docker-entrypoint-initdb.d
ENV MYSQL_ROOT_PASSWORD='rootpassword'
As stated in the documentation on the official docker website:
When a container is started for the first time, a new database with
the specified name will be created and initialized with the provided
configuration variables. Furthermore, it will execute files with
extensions .sh, .sql and .sql.gz that are found in
/docker-entrypoint-initdb.d. Files will be executed in alphabetical
order.
What you you would want to do is to modify Mysql's image entry point
and please note that you do not need to pass all the parameters, most of them are optional

Avoid hard coding the mysql container ip in my apache container script

I have a mysql container which runs fine. I can start it and see it up and running in the docker ps list.
I then try to run another learnitouch container in which an engine-db-seed.sh shell script tries to connect to the mysql container server.
The learnitouch container Dockerfile contains:
ENTRYPOINT ["/bin/bash", "/usr/bin/learnintouch/engine-db-seed.sh"]
The engine-db-seed.sh file contains:
/usr/bin/mysql/install/bin/mysql --protocol=tcp -h 172.17.0.2 -u root -proot -v < /usr/bin/learnintouch/db_engine-db.sql
The db_engine-db.sql is being seeded all right in the mysql database.
But I had to hard code the mysql container IP as you can see in the -h option. I got the 172.17.0.2 IP address from a docker inspect on the mysql container. Not the most automated way...
How can I do without such hard coding ?
Running the mysql container:
docker run -d -p 3306:3306 -v /home/stephane/dev/php/learnintouch/docker/mysql/data:/usr/bin/mysql/install/data --name mysql stephane/mysql:5.6.30
Running the learnintouch container:
docker run -p 127.0.0.1:80:80 --link mysql:mysql --name learnintouch stephane/learnintouch
I'm using Docker version 1.12.1, build 23cf638
Just use the service name and make sure that both services are running on the same network (bridge0 by default).
So if you create your mysql service like this
docker run -d --name foo mysql-image
your engine-db-seed.sh could then be
/usr/bin/mysql/install/bin/mysql --protocol=tcp -h foo -u root -proot -v < /usr/bin/learnintouch/db_engine-db.sql
mysql will make a dns request for foo which will be resolved by Docker to the ip of your foo service.

Connecting to Docker container from host

I just pulled and run the official Docker MySQL image and have it running locally on my machine:
docker run --name mydb -e MYSQL_ROOT_PASSWORD=12345 -d mysql:5.7.11
The instructions on that screen tell you how to connect to the MySQL server (container) from inside yet another container (which is configured as a command-line client). But I have a generic JDBC fat client (SQuirreL) and am wondering how to connect to my docker container.
For the JDBC connection string, I need to provide both a hostname and a dbname, what can I use? I would imagine the Docker container is somehow addressable from my host OS, but I haven't actually created any databases yet, so I'm not sure what dbname value I can supply:
jdbc:mysql://<hostname>:3306/<dbname>
You could run your instance with forwarding 3306:
$ docker run --expose=3306 -p 3306 mysql
See incoming ports.
The you specify:
jdbc:mysql://127.0.0.1:3306/<dbname>
You command become:
$ docker run --name mydb -e MYSQL_ROOT_PASSWORD=12345 -d --expose=3306 -p 3306 mysql:5.7.11
You might need to change the MySQL configuration.
Can go inside the container with:
$ docker exec -it mydb bash
And then you could:
$ echo "bind-address = 0.0.0.0" >> /etc/mysql/my.cnf
Don't forget to reload mysql.
Then you have to create the database and import your schema (if needed).
$ mysql -uroot -p12345 -e"CREATE DATABASE mydb"
$ mysql -uroot -p12345 mydb < mydb-schema.sql