Followers

Dockerfile mysql example

  Creating a Dockerfile for a MySQL database with tables involves several steps. Here's a basic example of a Dockerfile that sets up a M...

 Creating a Dockerfile for a MySQL database with tables involves several steps. Here's a basic example of a Dockerfile that sets up a MySQL container and creates a database table when the container is built and run:

# Use the official MySQL Docker image as the base image
FROM mysql:latest

# Set environment variables for MySQL root user password and database name
ENV MYSQL_ROOT_PASSWORD=root_password
ENV MYSQL_DATABASE=mydb

# Copy SQL script to initialize the database and table
COPY init.sql /docker-entrypoint-initdb.d/

# Expose the MySQL port
EXPOSE 3306

In this example:

  1. We start with the official MySQL Docker image as the base image.

  2. We set environment variables MYSQL_ROOT_PASSWORD and MYSQL_DATABASE to configure the MySQL server's root password and the name of the database you want to create.

  3. We copy an SQL script named init.sql into the /docker-entrypoint-initdb.d/ directory. This directory is special in the official MySQL image, and any SQL files placed here will be executed when the container is first started. You can customize init.sql with your specific SQL commands to create tables, insert data, etc. For example:

-- init.sql

-- Create a sample table
CREATE TABLE IF NOT EXISTS employees (
    id INT AUTO_INCREMENT PRIMARY KEY,
    first_name VARCHAR(50),
    last_name VARCHAR(50),
    email VARCHAR(100)
);

-- Insert some sample data
INSERT INTO employees (first_name, last_name, email)
VALUES
    ('John', 'Doe', 'john.doe@example.com'),
    ('Jane', 'Smith', 'jane.smith@example.com');
  1. We expose port 3306, which is the default MySQL port, to allow connections to the MySQL server from outside the container.

To build and run the Docker container using this Dockerfile:

  1. Save the Dockerfile and the init.sql script in the same directory.

  2. Open a terminal and navigate to the directory containing the Dockerfile and SQL script.

  3. Build the Docker image:

docker build -t mysql-custom .

Replace mysql-custom with your desired image name.

  1. Run the Docker container:
docker run -d -p 3306:3306 --name mysql-container mysql-custom

This command will create and start a Docker container named mysql-container based on the mysql-custom image, mapping port 3306 from the container to the host.

Now, you should have a MySQL container running with your custom database table inside it. You can connect to it using a MySQL client and the configured credentials.

COMMENTS

Name

Ansible,6,AWS,1,Azure DevOps,1,Containerization with docker,2,DevOps,2,Docker Quiz,1,Docker Swarm,1,DockerCompose,1,ELK,2,git,2,Jira,1,Kubernetes,1,Kubernetes Quiz,5,SAST DAST Security Testing,1,SonarQube,3,Splunk,2,vagrant kubernetes,1,YAML Basics,1,
ltr
item
DevOpsWorld: Dockerfile mysql example
Dockerfile mysql example
DevOpsWorld
https://www.devopsworld.co.in/2023/10/dockerfile-mysql-example.html
https://www.devopsworld.co.in/
https://www.devopsworld.co.in/
https://www.devopsworld.co.in/2023/10/dockerfile-mysql-example.html
true
5997357714110665304
UTF-8
Loaded All Posts Not found any posts VIEW ALL Readmore Reply Cancel reply Delete By Home PAGES POSTS View All RECOMMENDED FOR YOU LABEL ARCHIVE SEARCH ALL POSTS Not found any post match with your request Back Home Sunday Monday Tuesday Wednesday Thursday Friday Saturday Sun Mon Tue Wed Thu Fri Sat January February March April May June July August September October November December Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec just now 1 minute ago $$1$$ minutes ago 1 hour ago $$1$$ hours ago Yesterday $$1$$ days ago $$1$$ weeks ago more than 5 weeks ago Followers Follow THIS PREMIUM CONTENT IS LOCKED STEP 1: Share to a social network STEP 2: Click the link on your social network Copy All Code Select All Code All codes were copied to your clipboard Can not copy the codes / texts, please press [CTRL]+[C] (or CMD+C with Mac) to copy Table of Content